package com.mediacloud.app.newsmodule.fragment

import android.content.Context
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.kt.TypeX
import com.mediacloud.app.newsmodule.utils.VideoAddressGet
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig
import com.yoake.media.player.utils.XVideoBitRate
import com.yoake.media.player.utils.XVideoItem
import com.yoake.media.player.utils.cache.XPlayerPreloadManager
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.json.JSONObject

object ListPreLoadTool {
    @JvmStatic
    fun preLoad(context: Context, articleList: MutableList<ArticleItem>) {
        GlobalScope.launch {
            val size = articleList.size
            for (k in 0 until size) {
                val item = articleList[k]
                if (item.type == TypeX.NEWS.VIDEO) {
                    if (item.cmsCustomStyle == null) break
                    val type = item.cmsCustomStyle.getType()
                    when (type) {

                        1, 7, 11 -> {
                            try {
                                val video = JSONObject(item.video)
                                val tsAddress = VideoAddressGet.getAddress(video)
                                if (tsAddress != null) {
                                    val length = tsAddress.length()
                                    val videoPlayObj = XVideoItem(item.getTitle())
                                    videoPlayObj.cdnIv = AppFactoryGlobalConfig.ServerAppConfigInfo.OtherConfig.vms_cdn_iv
                                    videoPlayObj.cdnKey = AppFactoryGlobalConfig.ServerAppConfigInfo.OtherConfig.vms_cdn_key
                                    val cdnEncypt = video.optString("cdnConfigEncrypt")
                                    for (i in 0 until length) {
                                        val line = tsAddress.optJSONObject(i)
                                        val lineItem = XVideoBitRate()
                                        lineItem.path = line.optString("url")
                                        lineItem.title = line.optString("title")
                                        lineItem.cdnEncypt = cdnEncypt
                                        videoPlayObj.bitRates.add(lineItem)
                                    }
                                    val iterator = videoPlayObj.bitRates.iterator()
                                    while (iterator.hasNext()) {
                                        val v = iterator.next()
                                        if (v.title?.contains("源码") == true) {
                                            iterator.remove()
                                        }
                                    }
                                    videoPlayObj.bitRates.reverse()
                                    if (videoPlayObj.bitRates.size > 0) {
                                        val bitRates = videoPlayObj.bitRates
                                        val size = bitRates.size
                                        var tempBitRate: XVideoBitRate? = null
                                        for (i in 0 until size) {
                                            if ("${bitRates[i].title}".uppercase().contains("1080P")) {
                                                tempBitRate = bitRates[i]
                                                break
                                            }
                                        }
                                        if (tempBitRate == null) {
                                            tempBitRate = bitRates[size - 1]
                                        }
                                        XPlayerPreloadManager.getInstance(context).addPreloadTask(tempBitRate.path)
                                        item.xVideoItem = videoPlayObj
                                    }
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
                        }
                    }

                }
            }
        }
    }


//    @JvmStatic
//    fun preLoad2(context: Context, articleList: MutableList<ArticleItem>) {
//        GlobalScope.launch {
//            val size = articleList.size
//            for (k in 0 until size) {
//                val item = articleList[k]
//                if (item.type == TypeX.NEWS.VIDEO) {
//                    if (item.cmsCustomStyle == null) break
//                    val type = item.cmsCustomStyle.getType()
//                    when (type) {
//
//                        1, 7, 11 -> {
//                            try {
//                                val video = JSONObject(item.video)
//                                val tsAddress = VideoAddressGet.getAddress(video)
//                                val length = tsAddress.length()
//                                val videoPlayObj = XVideoItem(item.getTitle())
//                                videoPlayObj.cdnIv = AppFactoryGlobalConfig.ServerAppConfigInfo.OtherConfig.vms_cdn_iv
//                                videoPlayObj.cdnKey = AppFactoryGlobalConfig.ServerAppConfigInfo.OtherConfig.vms_cdn_key
//                                val cdnEncypt = video.optString("cdnConfigEncrypt")
//                                for (i in 0 until length) {
//                                    val line = tsAddress.optJSONObject(i)
//                                    val lineItem = XVideoBitRate()
//                                    lineItem.path = line.optString("url")
//                                    lineItem.title = line.optString("title")
//                                    lineItem.cdnEncypt = cdnEncypt
//                                    videoPlayObj.bitRates.add(lineItem)
//                                }
//                                val iterator = videoPlayObj.bitRates.iterator()
//                                while (iterator.hasNext()) {
//                                    val v = iterator.next()
//                                    if (v.title?.contains("源码") == true) {
//                                        iterator.remove()
//                                    }
//                                }
//                                videoPlayObj.bitRates.reverse()
//                                if (videoPlayObj.bitRates.size > 0) {
//                                    XPlayerPreloadManager.getInstance(context).addPreloadTask(videoPlayObj.bitRates[0].path)
//                                    item.xVideoItem = videoPlayObj
//                                }
//                            } catch (e: Exception) {
//                                e.printStackTrace()
//                            }
//                        }
//                    }
//
//                }
//            }
//        }
//    }


    @JvmStatic
    fun preLoad2(context: Context, articleList: MutableList<ArticleItem>) {
        GlobalScope.launch {
            val size = articleList.size
            for (k in 0 until size) {
                val item = articleList[k]
                if (item.video != null) {//跟ios保持一致，只要有video就处理

                    try {
                        val video = JSONObject(item.video)
                        val tsAddress = VideoAddressGet.getAddress(video)
                        if (tsAddress != null) {
                            val length = tsAddress.length()
                            val videoPlayObj = XVideoItem(item.getTitle())
                            videoPlayObj.cdnIv = AppFactoryGlobalConfig.ServerAppConfigInfo.OtherConfig.vms_cdn_iv
                            videoPlayObj.cdnKey = AppFactoryGlobalConfig.ServerAppConfigInfo.OtherConfig.vms_cdn_key
                            val cdnEncypt = video.optString("cdnConfigEncrypt")
                            for (i in 0 until length) {
                                val line = tsAddress.optJSONObject(i)
                                val lineItem = XVideoBitRate()
                                lineItem.path = line.optString("url")
                                lineItem.title = line.optString("title")
                                lineItem.cdnEncypt = cdnEncypt
                                videoPlayObj.bitRates.add(lineItem)
                            }
                            val iterator = videoPlayObj.bitRates.iterator()
                            while (iterator.hasNext()) {
                                val v = iterator.next()
                                if (v.title?.contains("源码") == true) {
                                    iterator.remove()
                                }
                            }
                            videoPlayObj.bitRates.reverse()
                            if (videoPlayObj.bitRates.size > 0) {
                                val bitRates = videoPlayObj.bitRates
                                val size = bitRates.size
                                var tempBitRate: XVideoBitRate? = null
                                for (i in 0 until size) {
                                    if ("${bitRates[i].title}".uppercase().contains("1080P")) {
                                        tempBitRate = bitRates[i]
                                        break
                                    }
                                }
                                if (tempBitRate == null) {
                                    tempBitRate = bitRates[size - 1]
                                }
                                XPlayerPreloadManager.getInstance(context).addPreloadTask(tempBitRate.path)
                                item.xVideoItem = videoPlayObj
                            }
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            }
        }
    }
}